package base.lib.adatper;


import android.databinding.BindingAdapter;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.kelin.mvvmlight.command.ReplyCommand;

import base.lib.widget.CellView;

import static base.lib.util.ActivityManager.getActivity;

/**
 * 王少岩 在 2017/3/21 创建了它
 */
public final class ViewBindingAdapter {

    @BindingAdapter({"toolbar_navigationIcon"})
    public static void setToolBarNavigation(Toolbar toolbar, int resId) {
        if (resId > 0)
            toolbar.setNavigationIcon(resId);
        else
            toolbar.setNavigationIcon(null);
    }

    @BindingAdapter({"naviCommand"})
    public static void naviCommand(Toolbar toolbar, final ReplyCommand clickCommand) {
        toolbar.setNavigationOnClickListener(v -> {
            if (clickCommand != null) {
                clickCommand.execute();
            }
        });
    }

    @BindingAdapter(value = {"clickCommand", "param"})
    public static void clickCommand(View view, final ReplyCommand<Object> clickCommand, Object object) {
        view.setOnClickListener(v -> {
            if (clickCommand != null) {
                clickCommand.execute(object);
            }
        });
    }

    @BindingAdapter({"my_drawableLeft"})
    public static void setMyDrableLeft(TextView textView, int resId) {
        textView.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);
    }

    @BindingAdapter({"my_background"})
    public static void setMyBackground(Toolbar toolbar, int color) {
        toolbar.setBackgroundColor(color);
    }

    @BindingAdapter({"my_textColor"})
    public static void setMyTextColor(TextView textView, int color) {
        textView.setTextColor(color);
    }

    @BindingAdapter({"my_infoText"})
    public static void setMyInfoText(CellView cellView, String infoText) {
        cellView.setInfoText(infoText);
    }

    @BindingAdapter({"my_infoHint"})
    public static void setMyInfoHint(CellView cellView, String hintText) {
        cellView.setInfoHint(hintText);
    }

    @BindingAdapter({"my_labelText"})
    public static void setMyLabelText(CellView cellView, String labelText) {
        cellView.setLabelText(labelText);
    }

    @BindingAdapter({"my_leftIcon"})
    public static void setMyLeftIcon(CellView cellView, int resId) {
        if (resId != 0)
            cellView.setLeftIcon(getActivity().getResources().getDrawable(resId));
        else
            cellView.setLeftIcon(null);
    }

    @BindingAdapter({"my_rightIcon"})
    public static void setMyRightIcon(CellView cellView, int resId) {
        if (resId != 0)
            cellView.setRightIcon(getActivity().getResources().getDrawable(resId));
        else
            cellView.setRightIcon(null);
    }

    @BindingAdapter({"my_clickCommand"})
    public static void myClickCommand(CellView cellView, ReplyCommand command) {
        cellView.setOnClickListener(v -> {
            if (command != null)
                command.execute();
        });
    }


    @BindingAdapter({"app:selected"})
    public static void setSelected(ImageView imageView, boolean isSelect){
        imageView.setSelected(isSelect);
    }


    @BindingAdapter({"app:view_adapter"})
    public static void viewAdapter(ViewPager viewPager, PagerAdapter adapter){
        viewPager.setAdapter(adapter);
    }

//    @BindingAdapter({"app:backgroundColor"})
//    public static void backgroundColor(TextView textView, Integer color){
//        textView.setBackgroundColor(getActivity().getResources());
//    }


    @BindingAdapter({"app:loadUrl"})
    public static void loadUrl(WebView webView, String url){
        if (Build.VERSION.SDK_INT >= 19) {
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  //设置 缓存模式
        // 开启 DOM storage API 功能
        webView.getSettings().setDomStorageEnabled(true);
        //开启 database storage API 功能
        webView.getSettings().setDatabaseEnabled(true);
        String cacheDirPath = getActivity().getFilesDir().getAbsolutePath() + "webcache";
        //设置数据库缓存路径
        webView.getSettings().setDatabasePath(cacheDirPath);
        //设置  Application Caches 缓存目录
        webView.getSettings().setAppCachePath(cacheDirPath);
        //开启 webView Caches 功能
        webView.getSettings().setAppCacheEnabled(true);

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                return super.shouldOverrideUrlLoading(view, url);
                webView.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
////                super.onPageFinished(view, url);
//                LinearLayout.LayoutParams  params = (LinearLayout.LayoutParams) webView.getLayoutParams();
//                webView.setLayoutParams(params);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });
        webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                ((WebView)v).requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
        webView.loadUrl(url);
    }

    @android.databinding.BindingAdapter({"app:onPageSelectedCommand"})
    public static void editTextCommand(ViewPager viewPager,
                                       final ReplyCommand<Integer> onPageSelectedCommand) {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (onPageSelectedCommand != null) {
                    onPageSelectedCommand.execute(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

